package advert_api

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"giv-vue-blog/global"
	"giv-vue-blog/models"
	"giv-vue-blog/models/res"
)

// AdvertDelView 删除广告
// @Tags 广告管理
// @Summary 删除广告
// @Description 删除广告
// @Param data body models.DelIDList true "查询参数"
// @Router /api/advert [delete]
// @Produce json
// @Success 200 {object} res.Response{data=string}
func (AdvertApi) AdvertDelView(c *gin.Context) {
	// models.DelIDList 的定义的全局通用的根据id删除的一个结构体
	var ad models.DelIDList
	err := c.ShouldBindJSON(&ad)
	if err != nil {
		res.FailedWithError(err, &ad, c)
		return
	}
	var AdvertList []models.AdvertsModel
	count := global.DB.Find(&AdvertList, ad.DelIDList).RowsAffected
	if count == 0 {
		res.FailedWithMsg("不存在的广告", c)
		return
	}
	err = global.DB.Delete(&AdvertList).Error
	if err != nil {
		res.FailedWithMsg("删除失败", c)
		return
	}
	res.OkWithMsg(fmt.Sprintf("共删除 %d 条广告", count), c)
}
